package com.kitty.game.task.service.taskHandler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.config.NPC;
import com.kitty.game.config.TaskSet;
import com.kitty.game.confirm.service.handler.BuyFashionConfirmHandler;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.enter.TitleInfo;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**人物证道处理类万道袍*/
@Component
public class RoleZhengDaoTaskHandler extends TaskHandler {
    /**人物证道任务的任务id*/
    private int taskId = 22518;
    private int TAO5000 = 360*5000;
    @Override
    public TaskType getTaskType() {
        return TaskType.ROLE_ZHENGDAO;
    }

    public void handleRoleTaoUp(Role role, int tao) {
        //人物道行增加
        int zhengdaoLevel = role.getExtendBox().getZhengdaoLevel();
        if(zhengdaoLevel>=10){//以前写的是10 只能领取到5
            return;
        }
        if(tao<3600000+(TAO5000*zhengdaoLevel)){
            return;
        }
        TaskSet taskSet = SpringUtils.getTaskService().getTaskSet(taskId+zhengdaoLevel, role);
        acceptTask(role, taskSet);
    }
    public void lingquTaoReward(Role role, NPC npc) {
        int zhengdaoLevel = role.getExtendBox().getZhengdaoLevel();
        int tao = role.getTao();
        if(tao<3600000+(TAO5000*zhengdaoLevel)){
            return;
        }
        //删除当前任务
        TaskSet taskSet = SpringUtils.getTaskService().getTaskSet(taskId+zhengdaoLevel, role);
        forceResetTask(role, taskSet);

        if(zhengdaoLevel>=1){
            //删除原有时装
            removeEquip(role);
        }
        //等级+1
        role.getExtendBox().setZhengdaoLevel(zhengdaoLevel + 1);
        //发送时装
        SpringUtils.getBean(BuyFashionConfirmHandler.class).rewardFasionWanDao(role);
        //发送道行
        SpringUtils.getRoleService().addTao(role, 50*360*100*role.getExtendBox().getZhengdaoLevel());
        //发送称号
        String fasionName = SpringUtils.getBean(BuyFashionConfirmHandler.class).getNameByIndex(role.getExtendBox().getZhengdaoLevel(), role.getGender());
        String titleName = fasionName.replaceAll("袍","").replaceAll("衣","");
        TitleInfo titleInfo = new TitleInfo();
        titleInfo.setType(titleName);
        titleInfo.setTitle(titleName);
        SpringUtils.getRoleService().addTitle(role, titleInfo);
        //刷新角色证道任务阶段
        List<FiedValue> list = new ArrayList<>();
        list.add(new FiedValue(1086, role.getExtendBox().getZhengdaoLevel()));
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(role.getRoleId());
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, respUpdate);

        //发送公告
        String taskName = getTaskName(role.getExtendBox().getZhengdaoLevel());
        String msg = "恭喜#R%s#n完成了#Y%s#n，获得了特殊称号#Y%s#n以及专属时装#Y%s#n，这难道就是未来道王吗！";
        msg = String.format(msg,role.getName(),taskName,titleName,fasionName);
        SpringUtils.getBean(ChatService.class).sendAdnotice(msg);
    }
    public String getTaskName(int index){
        String name= "";
        switch (index){
            case 1:
                name = "证道—初识道源";
                break;
            case 2:
                name = "证道—道北风波";
                break;
            case 3:
                name = "证道—桃柳姻缘";
                break;
            case 4:
                name = "证道—轩辕护送";
                break;
            case 5:
                name = "证道—北海棋事";
                break;
            case 6:
                name = "证道—蓬莱鲛珠";
                break;
            case 7:
                name = "证道—龙窟寻道";
                break;
            case 8:
                name = "证道—幽谷奇闻";
                break;
            case 9:
                name = "证道—昆仑幻梦";
                break;
            case 10:
                name = "证道—绝人妖风";
                break;
            case 11:
                name = "证道—海底惊变";
                break;
            case 12:
                name = "证道—冰原雷劫";
                break;
            case 13:
                name = "证道—合身大道";
                break;

        }
        return name;
    }
    public void lingquTaoReward2(Role role, NPC npc) {
        int zhengdaoLevel = role.getExtendBox().getZhengdaoLevel();
        int tao = role.getTao();
        if(tao<3600000+(TAO5000*zhengdaoLevel)){
            return;
        }
        //删除当前任务
        TaskSet taskSet = SpringUtils.getTaskService().getTaskSet(taskId+zhengdaoLevel, role);
        forceResetTask(role, taskSet);
        //等级+1
        role.getExtendBox().setZhengdaoLevel(zhengdaoLevel + 1);
        //发送道行
        SpringUtils.getRoleService().addTao(role, 50*360*100*role.getExtendBox().getZhengdaoLevel());
        //刷新角色证道任务阶段
        List<FiedValue> list = new ArrayList<>();
        list.add(new FiedValue(1086, role.getExtendBox().getZhengdaoLevel()));
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(role.getRoleId());
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, respUpdate);
    }
    public void removeEquip(Role role){
        RoleEquip roleEquip = null;
        boolean flag = false;
        for (Map.Entry<Integer, RoleEquip> entry : role.getEquipBox().getEquips().entrySet()) {
            roleEquip = entry.getValue();
            if ((roleEquip.getPosition() >= 4001 && roleEquip.getPosition() <= 4050) || roleEquip.getPosition() == 31) {
                if(roleEquip.getName().contains("万道")){
                    flag = true;
                    break;
                }
            }
        }
        if(flag){
            SpringUtils.getBean(EquipService.class).delRoleEquip(roleEquip, role);
        }
    }
}
